<?php
/**
 * 发送到货短信通知
 *
 *
 */
class sendsms extends Controller
{

	/**
	 * 构造函数
	 *
	 * 登陆检验
	 */
	function __construct()
    {
        parent::Controller(); 
		if (!$this->session->userdata('logged_in')){          		
			redirect('login');
			exit();
		}
    }
    
	// --------------------------------------------------------------------

    /**
	 * 发送到货短信通知
	 *
	 *
	 */	
    function index()
    {

    	// 到站代码
    	$this->load->model('code_destination_site_model');
    	$data['destination_site'] = $this->code_destination_site_model->get_all();
           
        $data['header_title']="到货短信通知";
     
        $this->load->view('sendsms/sendsms/list',$data);
		 
    }
    
    /*
     * 显示要发送短信的货单
     */
    function  show()
    {
    	$start_time = $this->input->post('st');
    	$end_time	= $this->input->post('et');
    	//$checkin_site =   $this->input->post('checkin_site');
    	
    	$destination_site  =  $this->input->post('destination_site');
    	
  
    	$search = $this->input->post('search');
    	//计算查询条件
    	$options = array(
    			'conditions' => null,
    	);
    	
    	
    	
    	
    	$data = array();
    	
    	//
    	$destination_site_array = array('id'=>null,'name'=>null);
    	$report_type = 1;
    	$this->load->model('shipping_list_model');
		$data['shipping_list'] = $this->shipping_list_model->find_shipping_lists_report($start_time,$end_time,$report_type,$destination_site);
    		
    	$this->load->model('code_destination_site_model');
		$data['start_time']=$start_time;
    	$data['end_time'] =$end_time;
    	$destination_site_array = $this->code_destination_site_model->load($destination_site);
    	
    	$data['destination_site'] = $destination_site_array['name'] ;
    		 
    	
    	$this->load->view('sendsms/sendsms/show',$data);
 
     
    	    	 
    }
    /*
     * 发送短信
     */
    function send(){
    	ini_set('max_execution_time',160);
    	$this->load->helper('send');
    	$shipper_id = $this->input->post('id');
    	$http = 'http://api.sms.cn/mtutf8/';		//短信接口
    	$uid = 'btyywl';							//用户账号
    	$pwd = 'btyywl';							//密码
    	$this->load->model('shipping_list_model');
    	$i =$j =$k =0;
    	$msg="";
    	foreach ( $shipper_id as $v)
    	{
    		$data = $this->shipping_list_model->loadv($v);
    		$mobile = $data['receiver_telephone'];
    		$receiver = $data['receiver'];
    		$goods_name= $data['goods_name'];
    		$destination_site =$data['destination_site_name'];
    		$destination_site_address  =$data['destination_site_address'];
    		$destination_site_telelphone  =$data['destination_site_telephone'];
    		$content = $receiver.",您的货物已到达".$destination_site."，请及时携带身份证到物流站提货，地址：".$destination_site_address."电话：".$destination_site_telelphone; 
 //   		echo $content = "$receiver，您的货物：$goods_name已经到达$destination_site,请于三日内携带身份证到物流站提货，地址：$destination_site_address,电话:$destination_site_telephone-[岳英物流]";

    		$mobileids	 = '';	//号码唯一编号
    		
    		//即时发送
    		$res = sendSMS($http,$uid,$pwd,$mobile,$content,$mobileids);
    	 
    		$result = explode("&",$res);
            $k++;
    		if (substr($result[1],5) == "100")
    		{
    			
    			$i++;
    		}else 
    		{
    			$j++;
    			$msg.=$mobile.",发送失败,原因:".substr($result[1],5).substr($result[2],8)."<br>";
    		}		
    	 
    	}
    	 $msg_result = "发送总数".$k."成功".$i."条，失败".$j."条";
    	 $data1['msg1'] = $msg_result;
    	 $data1['msg2'] = $msg;
    	 $this->load->view('sendsms/sendsms/send',$data1);
  
      	
    	
    }
    
  
}